﻿#include "DataClipperGenerator.h"
#include <vtkPlane.h>
#include <vtkActor.h>
#include <vtkClipPolyData.h>
#include <vtkDataSetMapper.h>
#include <vtkDataSetSurfaceFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkLookupTable.h>
#include <vtkDataSet.h>

namespace pst
{
    DataClipperGenerator::DataClipperGenerator(QObject* parent)
        : QObject(parent)
        , m_planeNormalDirection{ 0 }
        , m_planeOrigin{ 0.,0.,0. }
        , m_clipPlane(vtkSmartPointer<vtkPlane>::New())
        , m_surfaceFilter(vtkSmartPointer<vtkDataSetSurfaceFilter>::New())
        , m_clipFilter(vtkSmartPointer<vtkClipPolyData>::New())
        , m_mapper(vtkSmartPointer<vtkPolyDataMapper>::New())
        , m_actor(vtkSmartPointer<vtkActor>::New())
    {
        initPipeLine();
    }

    DataClipperGenerator::~DataClipperGenerator()
    {
    }

    void DataClipperGenerator::setInputData(vtkDataSet* inputDatas)
    {
        m_dataSetList = inputDatas;
    }

    vtkDataSet* DataClipperGenerator::getOutputData()
    {
        return m_dataSetList;
    }

    void DataClipperGenerator::setPlaneNormalDirection(double x, double y, double z)
    {
        m_planeNormalDirection[0] = x;
        m_planeNormalDirection[1] = y;
        m_planeNormalDirection[2] = z;
    }

    double* DataClipperGenerator::getPlaneNormalDirection()
    {
        return m_planeNormalDirection;
    }

    void DataClipperGenerator::setPlaneOrigin(double posx, double posy, double posz)
    {
        m_planeOrigin[0] = posx;
        m_planeOrigin[1] = posy;
        m_planeOrigin[2] = posz;
    }

    double* DataClipperGenerator::getPlaneOrigin()
    {
        return m_planeOrigin;
    }

    void DataClipperGenerator::setLookupTable(vtkLookupTable* lut)
    {
        m_lookupTable = lut;
    }

    vtkLookupTable* DataClipperGenerator::getLookupTable()
    {
        return m_lookupTable;
    }

    void DataClipperGenerator::update()
    {
        if (m_dataSetList == nullptr)
        {
            return;
        }

        m_surfaceFilter->SetInputData(m_dataSetList);
        m_surfaceFilter->Update();
        m_clipPlane->SetNormal(m_planeNormalDirection);
        m_mapper->SetLookupTable(m_lookupTable);
        //m_clipPlane->SetNormal(m_planeNormalDirection, 0, 0);
        m_clipPlane->SetOrigin(m_planeOrigin);
        m_clipFilter->Update();
        m_mapper->SetLookupTable(m_lookupTable);

    }

    vtkSmartPointer<vtkActor> DataClipperGenerator::getClipActor()
    {
        return m_actor;
    }

    void DataClipperGenerator::initPipeLine()
    {
        //m_surfaceFilter->SetInputConnection();
        //clipPlane->SetOrigin(0.0, 0.0, 0.0);
        //clipPlane->SetNormal(1.0, 0.0, 0.0);
        m_clipFilter->SetInputConnection(m_surfaceFilter->GetOutputPort());
        m_clipFilter->SetClipFunction(m_clipPlane);
        // 设置 InsideOut 为 false，表示保留平面正法线方向一侧的数据
        m_clipFilter->SetInsideOut(false);
        //m_clipFilter->Update();
        m_mapper->SetInputConnection(m_clipFilter->GetOutputPort());
        //m_mapper->SetLookupTable(m_lookupTable);
        m_mapper->SetInterpolateScalarsBeforeMapping(true);
        m_mapper->SetColorModeToMapScalars();
        m_mapper->ScalarVisibilityOn();
        m_mapper->SetUseLookupTableScalarRange(1);
        m_actor->SetMapper(m_mapper);
    }
}
